package com.tenglong.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class JsonArrayTest {
    public static void main(String[] args) {
        Root user1 = new Root();
        user1.setName("张三");
        user1.setAge(18);
        List<String> habits1 = new ArrayList<String>();
        habits1.add("抽烟");
        habits1.add("喝酒");
        user1.setHabits(habits1);

        Root user2 = new Root();
        user2.setName( "李四");
        user2.setAge(18);
        List<String> habits2 = new ArrayList<String>();
        habits2.add("打牌");
        habits2.add("唱歌");
        user2.setHabits(habits2);

        List<Root> users=new ArrayList<Root>();
        users.add(user1);
        users.add(user2);

        JSONArray jsonArray=new JSONArray();//1、创建JSONArray
        jsonArray.addAll(users);//2、把JSONArray添加List,List<User>转JSONArray
        System.out.println(jsonArray.get(0));//输出结果|com.godcin.test.bean.User@14ae5a5
        System.out.println(jsonArray.get(1));//输出结果|com.godcin.test.bean.User@7f31245a
        System.out.println("姓名："+((Root)jsonArray.get(0)).getName());//输出结果|姓名：张三
        System.out.println("爱好："+((Root)jsonArray.get(1)).getAge());//输出结果|爱好：[打牌, 唱歌]

        String jsonStr=jsonArray.toJSONString();//3、JSONArray转化json字符串
        System.out.println(jsonStr);//输出结果|[{"age":18,"habits":["抽烟","喝酒"],"name":"张三"},{"age":18,"habits":["抽烟","喝酒"],"name":"张三"}]

        users= JSON.parseArray(jsonStr, Root.class);//json字符串转List<User>
        System.out.println(users);//输出结果 |[com.godcin.test.bean.User@16f65612, com.godcin.test.bean.User@311d617d]
    }


//    HSSFWorkbook wb = new HSSFWorkbook();
//
//    HSSFSheet sheet = wb.createSheet("table");  //创建table工作薄
//    Object[][] datas = {{"时间","车牌号", "客户姓名", "联系方式"}, {"2018" , "豫A88C58",  "里斯","15847584759"},{"2018" , "豫A88C58",  "张三","15847584759"},{"2018" , "豫A88C58",  "张三","15847584759"}};
//    HSSFRow row;
//    HSSFCell cell;
//        for(int i = 0; i < datas.length; i++) {
//        row = sheet.createRow(i);//创建表格行
//        for(int j = 0; j < datas[i].length; j++) {
//            cell = row.createCell(j);//根据表格行创建单元格
//            cell.setCellValue(String.valueOf(datas[i][j]));
//        }
//    }
//        wb.write(new FileOutputStream("F:\\table.xls"));
}
